using System;
using System.Text.RegularExpressions;
using System.Web;
namespace HKB.Common
{
	public class SetGetCookies
	{
		public static HttpCookie Get(string cookieKey)
		{
			return HttpContext.Current.Request.Cookies[cookieKey];
		}
		public static string GetValue(string cookieKey)
		{
			string result = null;
			HttpCookie httpCookie = HttpContext.Current.Request.Cookies[cookieKey];
			if (httpCookie != null)
			{
				result = httpCookie.Value;
			}
			return result;
		}
		public static void Set(string cookieKey, string cookieValue)
		{
			SetGetCookies.Set(cookieKey, cookieValue, new System.DateTime?(System.DateTime.MinValue), "");
		}
		public static void Set(string cookieKey, string itemKey, string cookieValue)
		{
			SetGetCookies.Set(cookieKey, itemKey, cookieValue, null, "");
		}
		public static void Set(string cookieKey, string cookieValue, System.DateTime? expires, string domain)
		{
			HttpCookie httpCookie;
			if (SetGetCookies.IsChinese(cookieValue))
			{
				httpCookie = new HttpCookie(cookieKey, HttpUtility.UrlEncode(cookieValue));
			}
			else
			{
				httpCookie = new HttpCookie(cookieKey, cookieValue);
			}
			if (!string.IsNullOrEmpty(domain))
			{
				httpCookie.Domain = domain;
			}
			httpCookie.Path = "/";
			if (expires.HasValue && expires.Value != System.DateTime.MinValue)
			{
				httpCookie.Expires = expires.Value;
			}
			HttpContext.Current.Response.Cookies.Add(httpCookie);
		}
		public static void Set(string cookieKey, string itemKey, string cookieValue, System.DateTime? expires, string domain)
		{
			HttpCookie httpCookie = HttpContext.Current.Response.Cookies[cookieKey];
			httpCookie[itemKey] = cookieValue;
			if (!string.IsNullOrEmpty(domain))
			{
				httpCookie.Domain = domain;
			}
			httpCookie.Path = "/";
			if (expires.HasValue)
			{
				httpCookie.Expires = expires.Value;
			}
		}
		public static void Remove(string cookieKey)
		{
			SetGetCookies.Remove(cookieKey, "");
		}
		public static void Remove(string cookieKey, string domain)
		{
			HttpCookie httpCookie = HttpContext.Current.Request.Cookies[cookieKey];
			if (httpCookie != null)
			{
				httpCookie.Expires = System.DateTime.Now.AddDays(-1.0);
				if (!string.IsNullOrEmpty(domain))
				{
					httpCookie.Domain = domain;
				}
				httpCookie.Path = "/";
				HttpContext.Current.Response.Cookies.Add(httpCookie);
			}
		}
		public static bool IsChinese(string input)
		{
			bool result = false;
			for (int i = 0; i < input.Length; i++)
			{
				Regex regex = new Regex("^[一-龥]$");
				if (regex.IsMatch(input[i].ToString()))
				{
					result = true;
					break;
				}
			}
			return result;
		}
	}
}
